************************************
*** FIGURE 6: Clinic Efficiency Measures
*** FIGURE A10: Clinic-Level Average Dispensing Interval
*** FIGURE A11: Appointments Intervals (in Months) and Patient Lapse

*** TABLE 2: Medical Care Provided at Visit
*** TABLE A13: Medical Care Provided at Visit, Without Controls
*** TABLE A23: Average Appointment Intervals and Clinic-Level Outcomes
*** TABLE A24: Patient-Level Predictors of Appointment Intervals
*** TABLE A25: Appointment Intervals and Patient Outcomes

************************************

use Data/Analysis/Data_ClinicQuarters, clear

keep if QPostEMR<=8 & QPostEMR>=0
forvalues l = 1/8 {
	gen L`l'event = QPostEMR==`l'
}

tab VisitDay
replace VisitDay=VisitDay/91
replace RefillDay=RefillDay/91

foreach y of varlist logVisits VisitDay RefillDay InitDay lognum_staff_treat {
	reghdfe `y' L*event, absorb(VisitSite) cluster(VisitSite)
	estimates store e_`y'
}


local date = 20240906
event_plot e_logVisits e_VisitDay e_RefillDay e_lognum_staff_treat, ///
		stub_lag(L#event L#event L#event) stub_lead() plottype(scatter) ciplottype(rspike) ///
		together perturb(-0.3(0.2)0.3) trimlead() noautolegend ///
		graph_opt( ///
			xtitle("Quarter Index") ytitle("Coefficient Estimates") xlabel(0(1)8) ylabel(-0.2(0.2)0.4) ///
			legend(pos(6) order(1 "log(Number of Visits)" 3 "Fraction of Days with Visits" 5 "Fraction of Days with Refill Visits" 7 "log(Number of Staff)" ///
			) rows(2) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(cranberry)) lag_ci_opt1(color(cranberry)) ///
		lag_opt2(msymbol(D) color(erose)) lag_ci_opt2(color(erose)) ///
		lag_opt3(msymbol(T) color(orange)) lag_ci_opt3(color(orange))  ///
		lag_opt4(msymbol(S) color(purple)) lag_ci_opt4(color(purple)) 
graph save "Output/Figures/Figure06PanelA.gph", replace
graph export "Output/Figures/Figure06PanelA.png", replace


use Data/Analysis/Data_ClinicYears, clear

replace ARVInit=ARVInit/30

keep YearsPostEMR EMRyear Datey VisitSite ARVInit  

gen Ei=EMRyear
gen t=Datey
gen K = t-Ei 								
gen D = K>=0 & Ei!=. 

gen i=VisitSite


forvalues l = 0/6 {
	gen L`l'event = K==`l'
}
replace L6event=1 if K>6
forvalues l = 1/6 {
	gen F`l'event = K==-`l'
}
drop F1event


foreach y of varlist ARVInit {
	reghdfe `y' F*event L*event, a(i t) cluster(i)
	estimates store e_`y'
	}

event_plot e_ARVInit, ///
		stub_lag(L#event L#event L#event) stub_lead(F#event F#event F#event) plottype(scatter) ciplottype(rspike) ///
		together  trimlead(6) noautolegend ///
		graph_opt( ///
			xtitle("Year Index") ytitle("Coefficient Estimates") ///
					xlabel(-6 "-6" -5 "-5" -4 "-4" -3 "-3" -2 "-2" -1 "-1" 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "{&ge} 6")  ylabel(-0.4(0.2)0.4) ///
			legend(pos(6) order(1 "Average Interval (Months)" ///
					) rows(2) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black)) 
	graph save "Output/Figures/Figure06PanelB.gph", replace
	graph export "Output/Figures/Figure06PanelB.png", replace




	
use Data/Analysis/Data_Individual, clear

*Focus on those who initiate as children and return for a refill within six months
gen Sample=0
replace Sample=1 if AgeInit<18
*We can only observe this return if they initiate after EMR, or within six months before
replace Sample=0 if InitDate<EMRDate-180
*6 month refill
gen SR=VisitDate if ART==2
replace SR=. if VisitDate>InitDate+180
replace SR=. if VisitDate<InitDate+90
gsort PatientId VisitDate
by PatientId: egen SixRef=max(SR)


replace Sample=0 if SixRef>InitDate+180

replace Sample=0 if VisitDate!=SixRef

replace Post=(InitDate>=EMRDate)

gen Post_UI=Post*UnderweightInit
gen Post_NUI=Post*(1-UnderweightInit)

local covars "Male Age_09 UnderweightInit AdvHIVInit"

gen Post_hospital=Post*hospital



cap erase Output/Tables/Table02.xls
cap erase Output/Tables/Table02.txt

replace Sample=0 if UnderweightInit!=1
local covars "Male Age_09 AdvHIVInit"
reghdfe Underweight Post `covars' if Sample==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/Table02.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 

reghdfe Underweight Post Post_hospital `covars' if Sample==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/Table02.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post Post_hospital) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 


cap erase Output/Tables/TableA13.xls
cap erase Output/Tables/TableA13.txt

reghdfe Underweight Post if Sample==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA13.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post Post_hospital) addtext(Year FE, Y, Clinic FE, Y, Controls, N) 
reghdfe Underweight Post Post_hospital if Sample==1, a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA13.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post Post_hospital) addtext(Year FE, Y, Clinic FE, Y, Controls, N) 



use Data/Analysis/Data_IndividualInit, clear

gen CD4Init=CD4Count!=.
replace CD4Init=0 if CD4Count==0

replace Encounter=0 if Encounter==.
gen Post_hospital=Post*hospital


local covars "Male Age_* UnderweightInit AdvHIVInit"
foreach y of varlist TBT Encounter CD4Init ARVInit {
	reghdfe `y' Post `covars', a(Datey VisitSite) cluster(VisitSite)
			outreg2 using Output/Tables/Table02.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 
	reghdfe `y' Post Post_hospital `covars', a(Datey VisitSite) cluster(VisitSite)
			outreg2 using Output/Tables/Table02.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post Post_hospital) addtext(Year FE, Y, Clinic FE, Y, Controls, Y) 

}

foreach y of varlist TBT Encounter CD4Init ARVInit UnderweightInit AdvHIVInit {
	reghdfe `y' Post, a(Datey VisitSite) cluster(VisitSite)
			outreg2 using Output/Tables/TableA13.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post Post_hospital) addtext(Year FE, Y, Clinic FE, Y, Controls, N) 
	reghdfe `y' Post Post_hospital, a(Datey VisitSite) cluster(VisitSite)
			outreg2 using Output/Tables/TableA13.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post Post_hospital) addtext(Year FE, Y, Clinic FE, Y, Controls, N) 
}



***FIGURE A11, TABLE A24, A25


use Data/Analysis/Data_Individual, clear

*Calculate number of months of doses provided at visit, assume no longer than 6 months (99% of observations)
gen ARVMonths = ARVDispensedDays/30 
replace ARVMonths=. if ARVMonths>6

*Use only post-EMR data
keep if InitDate > EMRDate

gsort PatientId VisitDate
gen DefaultLast=Default[_n-1]
replace DefaultLast=. if PatientId!=PatientId[_n-1]

label variable ARVMonths "Interval in Months"
label variable Age_09 "Age 0 to 9"
label variable Age_1017 "Age 10 to 17"
label variable Age_1849 "Age 18 to 49"
label variable Male "Male"
label variable AdvHIVInit "Advanced HIV at Initiation"
label variable UnderweightInit "Underweight at Initiation"
label variable DefaultLast "Missed Last Appointment"
label variable YearsInit "Years Since Initiation"

cap erase Output/Tables/TableA24.xls
cap erase Output/Tables/TableA24.txt

reghdfe ARVMonths Age_* Male AdvHIVInit UnderweightInit if Init==1, a(Datey VisitSite) 
		estimates store e_ARVMonths
		outreg2 using Output/Tables/TableA24.xls, replace alpha(0.01, 0.05, 0.1) symbol(***, **, *)  keep(ARVMonths Age_09 Age_1017 Age_1849 Male AdvHIVInit UnderweightInit) nocons label addtext(Year FE, Y, Clinic FE, Y, Split, "At Initiation") 
		
		reghdfe ARVMonths Age_* Male AdvHIVInit UnderweightInit DefaultLast YearsInit, a(Datey VisitSite) 
		estimates store e_ARVMonths
		outreg2 using Output/Tables/TableA24.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(ARVMonths Age_09 Age_1017 Age_1849 Male AdvHIVInit UnderweightInit DefaultLast YearsInit) nocons label addtext(Year FE, Y, Clinic FE, Y, Split, "Later Visits") 
		
*TEXT ONLY: spacing variation at patient-visit level, for initiation and refill visits
tab ARVMonths if Init==1
*85% 1 month or less
tab ARVMonths if Init!=1

gsort PatientId VisitDate

gen OneYearVisit=(VisitDate<=InitDate+365)*(VisitDate>InitDate+180)
gen OYVD=VisitDate if VisitDate<=InitDate+365 & VisitDate>InitDate+180
by PatientId: egen InCareOneYear=max(OneYearVisit)
by PatientId: egen OneYearVisitDate=min(OYVD)
replace InCareOneYear=. if InitYear>2017
gen LapsedAfterOneYear=1-InCareOneYear

gen TwoYearVisit=(VisitDate<=InitDate+(2*365))*(VisitDate>InitDate+180+365)
gen TYVD=VisitDate if VisitDate<=InitDate+(2*365) & VisitDate>InitDate+180+365
by PatientId: egen InCareTwoYears=max(TwoYearVisit)
by PatientId: egen TwoYearVisitDate=min(TYVD)
replace InCareTwoYears=. if InitYear>2016
gen LapsedAfterTwoYears=1-InCareTwoYears

gen Spacing05=(ARVMonths<=0.5)
gen Spacing1=(ARVMonths<=1)*(ARVMonths>0.5)
gen Spacing2=(ARVMonths<=2)*(ARVMonths>1)

label variable Spacing05 "2 Weeks"
label variable Spacing1 "1 Month"
label variable Spacing2 "2 Months"
label variable Default "Missed Next Appointment"
label variable LapsedAfterOneYear "Lapsed 1 Year After Initiation"
label variable LapsedAfterTwoYears "Lapsed 2 Years After Initiation"


cap erase Output/Tables/TableA25.xls
cap erase Output/Tables/TableA25.txt

reghdfe Default Spacing* Age_* Male AdvHIVInit UnderweightInit if Init==1, a(Datey) 
		estimates store e_SpacingDefault
		outreg2 using Output/Tables/TableA25.xls, replace alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Spacing*) nocons label addtext(Year FE, Y, Controls, Y, Split, "At Initiation") 


reghdfe LapsedAfterOneYear Spacing* Age_* Male AdvHIVInit UnderweightInit if Init==1, a(Datey) 
		estimates store e_SpacingLapsed
		outreg2 using Output/Tables/TableA25.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Spacing*) nocons label addtext(Year FE, Y, Controls, Y, Split, "At Initiation") 


coefplot (e_SpacingDefault, label("Missed Next Appointment") msymbol(O) mcol(lavender) ciopts(recast(rcap) lcol(lavender))) ///
(e_SpacingLapsed, label("Lapsed 1 Year After Initiation") msymbol(T) mcol(purple) ciopts(recast(rcap) lcol(purple))) ///
, legend(position(6) rows(1)) keep(Spacing*)  xline(0, lpattern(dash) lcolor(black)) level(95) xtitle("Coefficient Estimates") ytitle("Interval") xlabel(-0.125(0.025)0.05) 
	graph save "Output/Figures/FigureA11PanelA.gph", replace
	graph export "Output/Figures/FigureA11PanelA.png", replace


*Extend spacing for follow-up visits
gen Spacing3=(ARVMonths<=3)*(ARVMonths>2)
gen Spacing4=(ARVMonths<=4)*(ARVMonths>3)

label variable Spacing3 "3 Months"
label variable Spacing4 "4 Months"

*Later visits
reghdfe Default Spacing* Age_* Male AdvHIVInit UnderweightInit DefaultLast if VisitDate==OneYearVisitDate, a(Datey) 
		estimates store e_SpacingDefault
		outreg2 using Output/Tables/TableA25.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Spacing*) nocons label addtext(Year FE, Y, Controls, Y, Split, "One Year After Initiation") 

reghdfe LapsedAfterTwoYears Spacing* Age_* Male AdvHIVInit UnderweightInit DefaultLast if VisitDate==OneYearVisitDate, a(Datey) 
		estimates store e_SpacingLapsed
		outreg2 using Output/Tables/TableA25.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Spacing*) nocons label addtext(Year FE, Y, Controls, Y, Split, "One Year After Initiation") 


coefplot (e_SpacingDefault, label("Missed Next Appointment") msymbol(O) mcol(lavender) ciopts(recast(rcap) lcol(lavender))) ///
(e_SpacingLapsed, label("Lapsed 2 Years After Initiation") msymbol(T) mcol(purple) ciopts(recast(rcap) lcol(purple))) ///
, legend(position(6) rows(1)) keep(Spacing*)   xline(0, lpattern(dash) lcolor(black)) level(95) xtitle("Coefficient Estimates") ytitle("Interval") xlabel(-0.05(0.025)0.1) 
graph save "Output/Figures/FigureA11PanelB.gph", replace
graph export "Output/Figures/FigureA11PanelB.png", replace



	
***FIGURE A10
	
	
replace LapsedAfterTwoYears=. if Init!=1

gen num_tests=TBT+Encounter+(CD4Count!=.)+(ViralSuppressed!=.)
gcollapse (sum) LapsedAfterTwoYears num_tests (mean) Male Age_* UnderweightInit AdvHIVInit ARVMonths, by(VisitSite PatientId Datey)
drop if Datey==2018
gcollapse (mean) num_tests (mean) LapsedAfterTwoYears Male Age_* UnderweightInit AdvHIVInit ARVMonths, by(VisitSite Datey)

*FIGURE: There is substantial variation in spacing across clinics -- this is average spacing across clinics
histogram ARVMonths if Datey==2017, bins(25) xtitle("Average Interval (Months)")
	graph save "Output/Figures/FigureA10.gph", replace
	graph export "Output/Figures/FigureA10.png", replace
	
	
***TABLE A23
	
	

label variable ARVMonths "Average Interval in Months"
label variable Age_09 "Age 0 to 9"
label variable Age_1017 "Age 10 to 17"
label variable Age_1849 "Age 18 to 49"
label variable Male "Male"
label variable AdvHIVInit "Advanced HIV at Initiation"
label variable UnderweightInit "Underweight at Initiation"
label variable num_tests "Annual Tests Per Patient"
label variable LapsedAfterTwoYears "Share Lapsed 2 Years After Initiation"

cap erase Output/Tables/TableA23.xls
cap erase Output/Tables/TableA23.txt

reghdfe num_tests ARVMonths, a(Datey)
		outreg2 using Output/Tables/TableA23.xls, replace alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(ARVMonths) label addtext(Year FE, Y, Controls, N) 
reghdfe num_tests ARVMonths Male Age_* UnderweightInit AdvHIVInit, a(Datey)
		outreg2 using Output/Tables/TableA23.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(ARVMonths) label addtext(Year FE, Y, Controls, Y)
reghdfe LapsedAfterTwoYears ARVMonths, a(Datey)
		outreg2 using Output/Tables/TableA23.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(ARVMonths) label addtext(Year FE, Y, Controls, N) 
reghdfe LapsedAfterTwoYears ARVMonths Male Age_* UnderweightInit AdvHIVInit, a(Datey)
		outreg2 using Output/Tables/TableA23.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(ARVMonths) label addtext(Year FE, Y, Controls, Y)


